/* eslint-disable eol-last */
/* eslint-disable indent */
'use strict';
const { formatBlog } = require('../utils/formatBlog');
const { formatUser } = require('../utils/formatUser');

module.exports = app => {
    class BlogService extends app.Service {
        // 5.创建微博，接收三个参数分别是  内容  地址 userId
        async create({ content, userId, images }) {
                // 6.写入到数据库中 创建
                const result = await this.app.model.Blog.create({
                    content,
                    userId,
                    images,
                });
                return result;
            }
            // 获取博客信息
        async getProfileBlogList({ userName, pageIndex = 0, pageSize }) {

            const result = await this.app.model.Blog.findAndCountAll({
                order: [
                    ['id', 'desc'],
                ], // 通过id降序排列
                limit: pageSize, // 显示的条数
                offset: pageIndex * pageSize,
                // 多表联查
                include: {
                    model: this.app.model.User, // 联查的表
                    attributes: ['nickName', 'avatar', 'userName'], // 规定 users 表显示的字段 昵称  头像
                    where: { // 联查条件
                        userName,
                    },
                },
            });

            const count = result.count;
            const blogList = result.rows.map(item => {
                item = formatBlog(item.dataValues);
                item.user = formatUser(item.user.dataValues);
                return item;
            });
            return { blogList, count };
        }

        // 获取所有人的博客数据
        async getSquareBlogList({ pageIndex = 0, pageSize }) {
                const result = await this.app.model.Blog.findAndCountAll({
                    order: [
                        ['createdAt', 'desc'],
                    ],
                    limit: pageSize,
                    offset: pageSize * pageIndex,
                    include: {
                        model: this.app.model.User,
                        attributes: ['nickName', 'avatar', 'userName'],
                    },
                });
                // 博客的总量
                const count = result.count;
                // 博客列表
                const blogList = result.rows.map(item => {
                    item = formatBlog(item.dataValues); // 格式化博客
                    item.user = formatUser(item.user.dataValues); // 格式化用户
                    return item;
                });
                return {
                    blogList, // 博客列表
                    count, // 博客总量
                };
            }
            //  获取关注人的博客数据
        async getFollowerBlogList({ userId, pageIndex = 0, pageSize = 10 }) {
            const result = await this.app.model.Blog.findAndCountAll({
                order: [
                    ['createdAt', 'desc'],
                ],
                limit: pageSize,
                offset: pageIndex * pageSize,
                include: [{ // 博客与用户对应
                        model: this.app.model.User,
                        attributes: ['id', 'userName', 'nickName', 'avatar'],
                    },
                    { // 博客与被关注人对应
                        model: this.app.model.UserRelation,
                        attributes: ['userId', 'followerId'],
                        where: { userId },
                    },
                ],
            });
            console.log(userId, pageIndex, pageSize);
            const count = result.count;
            // 博客列表
            let blogList = result.rows.map(row => row.dataValues);

            blogList = blogList.map(item => {
                item.user = formatUser(item.user.dataValues);
                item = formatBlog(item);
                return item;
            });
            return {
                count,
                blogList,
            };
        }
    }
    return BlogService;
};